#encoding:utf-8

'''
data=2017/1/17 下午3:12
__author__='binpo',
author_email='binpocn@163.com',
'''

import requests
from models import QueryRecord,Exchange
import json
import sys
from logs import LOG
log=LOG('exchange')
e_chat_id='1.2.328095'

class Config():
    wallet_host = "localhost"
    wallet_port = 8089
    wallet_user = "e-chat"
    wallet_password = "aa6vuCw84BxQa3uT"
    witness_url = "ws://127.0.0.1:5909"
    witness_user = "e-chat"
    witness_password = "aa6vuCw84BxQa3uT"
    account = "e-chat"

ETAO_URL='http://118.178.183.18:8081/etaoapi/edrRecharge/doRecharge'
ECHAT_URL='http://106.14.20.133:8081/echatapi/edrRecharge/doRecharge'


import hashlib
def sign(sign_str):
    return hashlib.md5(sign_str.encode(encoding='utf-8')).hexdigest()

from utils import get_current_price,get_usd_cyh
def make_request(obj):

    data={}
    session = obj.block_num
    account = obj.app_account
    amount = obj.amount
    amount = float(float(amount)/1000)
    
    price = get_current_price()
    chy = float(get_usd_cyh())
    
    amount = amount*(price* chy)
    amount = round(amount,3)
    # md5key = CWeWfP94a7LMjDeheD2jYSdxkNkjkpKi
    sign_str = "account={0}&amount={1}&session={2}&md5key=CWeWfP94a7LMjDeheD2jYSdxkNkjkpKi".format(account,amount,session)
    print(type(sign_str))
    print (sign_str)
    token = sign(sign_str)#'IxmbrnIrw6IgnL1I6A0LP7KDeueYgDECxbhiUCmbY/0sKLeWXPN9qqOzyWQnUuVPm7kXLX+i55+484DMo/kgHwJ1M9XNXoI9meJ7lzpzI4rKokOOBu2R8IAjuox3EeQHNLBAMMtTqEkKZUSyBq0+UzJIe7TH4wbNagqYz3F0SQY='#sign(sign_str)
    print(token)
    data={'session':session,'account':account,'amount':amount,'token':token}
    print (data)
    if obj.echange_type=='etao':
        res = requests.post(ETAO_URL,data=data)
    else:
        res = requests.post(ECHAT_URL,data=data)
    content = res.text
    print(content)
    content = json.loads(content)
    if content.get('code')=='OK':
        return True
    else:
        return False

def exchange_sender():
    try:
        exchange_list = Exchange.select().where(Exchange.status==False)
        for exchange in exchange_list:
            print('-------------------------------',exchange.id)
            content = make_request(exchange)
            print(content)
            if content:
                exchange.status = True
                exchange.save()
    except:
        log.error()
        t,v,tb = sys.exc_info()
        print(v)


# exit(0)
# exchange = Exchange()
# exchange.echange_type = 'e-chat'
# exchange.amount = 10
# exchange.app_account = '1000096'
# exchange.block_num='1999029'
# exchange.save()
#
# exit(0)

# sign_str = "account={0}&amount={1}&session={2}".format(exchange.app_account,exchange.amount,exchange.block_num)
# print sign_str
# token = sign(sign_str)
# print token
# make_request(exchange)

# obj=exchange
# session = obj.block_num
# account = obj.app_account
# amount = obj.amount
# sign_str = "account={0}&amount={1}&session={2}".format(account,amount,session)
# token = sign(sign_str)
# print token
# from Crypto.PublicKey import RSA
# from Crypto.Signature import PKCS1_v1_5
# from Crypto.Hash import SHA
# from base64 import b64decode
# #
# def rsa_verify():
#     public_key_file = open('rsa_publick.pem', 'r')
#     public_key = RSA.importKey(public_key_file)
#     sign = base64.b64decode(token)
#     h = SHA.new(sign_str)
#     verifier = PKCS1_v1_5.new(public_key)
#     print verifier.verify(h, sign)
#
if '__main__' == __name__:
    exchange_sender()